package com.xframe.xdal.core.listener;



import com.xframe.xdal.core.DataBaseAccess;
import com.xframe.xdal.core.exception.ListenerException;
import com.xframe.xdal.core.listener.mysql.MySqlListener;

/**
 * 工厂类 负责根据DataBaseAccess对象的数据库类型来创建对应的监听器
 *
 * @author xufeng
 */
public abstract class Factory {

    public static IDataBaseListener buildListener(DataBaseAccess dataBaseAccess, RowEventListener rowEventListener){
        IDataBaseListener dataBaseListener = null;
        switch (dataBaseAccess.getDataSource().getDataSourceTypeEnums()){
            case MySql:
                dataBaseListener = new MySqlListener(dataBaseAccess);
                break;
            //case Oracle:
                //dataBaseListener = new DBChangeNotification(dataBaseAccess);
                //break;
            //case SqlServer:
                //dataBaseListener = new SqlServerListener(dataBaseAccess);
                //break;
        }
        if(dataBaseListener == null){
            throw new ListenerException("不支持该数据源的监听" + dataBaseAccess.getDataSource().getDataSourceTypeEnums());
        }
        if(rowEventListener != null){
            dataBaseListener.addRowEventListener(rowEventListener);
        }
        return dataBaseListener;
    }
}
